package edu.scau.cxq.gp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import edu.scau.cxq.gp.common.annotation.ResponseResult;
import edu.scau.cxq.gp.entity.RecordIn;
import edu.scau.cxq.gp.service.IRecordInService;
import edu.scau.cxq.gp.entity.vo.RecordInVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @description 存储入库信息 前端控制器
 * @return
 * @author TLC
 */

@RestController
@ResponseResult
@RequestMapping("/record-in")
public class RecordInController {

    @Resource
    private IRecordInService recordInService;

    /**
     * 插入新入库信息
     * @param recordIn
     * @return
     */
    @PostMapping("/insertOne")
    @Transactional
    public boolean insertOne(@RequestBody RecordIn recordIn) {

        // 添加入库信息之前进行的一些操作
        if (!recordInService.beforeInsertRecordIn(recordIn)) {
            return false;
        }

        // 添加入库信息
        return recordInService.save(recordIn);
    }

//    /**
//     * 查找入库信息
//     * @return
//     */
//    @GetMapping("/select")
//    public List<RecordIn> select(@RequestBody RecordIn recordIn) {
//        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(recordIn);
//        List<RecordIn> recordIns = recordInService.list(queryWrapper);
//        return recordIns;
//    }
//
//    /**
//     * 分页查找入库信息
//     * @return
//     */
//    @GetMapping("/selectByPage")
//    public IPage<RecordIn> selectByPage (Integer current, Integer size, @RequestBody RecordIn recordIn) {
//        Page<RecordIn> page = new Page<>(current, size);
//        QueryWrapper queryWrapper = WrapperUtils.entitytoWrapper(recordIn);
//        return recordInService.page(page, queryWrapper);
//    }

    /**
     * 查找入库信息
     * @return
     */
    @GetMapping("/select")
    public PageInfo<RecordInVO> select(Integer currentPage, Integer size, @RequestParam(value = "goodsId", required = false) Long goodsId,
                                        @RequestParam(value = "supId", required = false) Long supId,
                                        @RequestParam(value = "wareId", required = false) Long wareId) {
        PageInfo<RecordInVO> recordInVOList = recordInService.getRecordInVOList(currentPage, size, goodsId, supId, wareId);
        return recordInVOList;
    }

    @PostMapping("rollback")
    public boolean rollback (@RequestBody RecordIn recordIn) {
        return recordInService.rollback(recordIn.getInId());
    }

    /**
     * 删除
     * @param cusId
     * @return
     */
    @PutMapping("/deleteOne")
    public boolean deleteOne(String cusId) {
        return recordInService.removeById(cusId);
    }

//    /**
//     * 修改入库信息
//     * @RequestBody：
//     * 作用：
//     * 主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的)；
//     * @param recordIn
//     * @return
//     */
//    @PutMapping("/updateOne")
//    public boolean updateOne (@RequestBody RecordIn recordIn) {
//        return recordInService.updateById(recordIn);
//    }
}
